import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class SearchPage extends StatefulWidget {
  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  final TextEditingController _searchController = TextEditingController();
  List<String> _searchHistory = ['奇遇']; // 历史搜索记录
  List<String> _hotSearchWords = ['从自设开始的捏图生活', '游戏资料包设计大赛']; // 热搜词

  // 模拟搜索功能
  void _performSearch(String query) {
    // 这里可以替换为实际的搜索逻辑，例如API调用
    setState(() {
      // 清空历史记录
      _searchHistory.insert(0, query);
      _searchHistory = _searchHistory.toSet().toList(); // 去重
    });
  }

  // 清除历史搜索记录
  void _clearSearchHistory() {
    setState(() {
      _searchHistory.clear();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('搜索页面'),
      ),
      body: Column(
        children: [
          // 搜索框
          Padding(
            padding: EdgeInsets.all(16.0),
            child: TextField(
              controller: _searchController,
              decoration: InputDecoration(
                labelText: '输入搜索内容',
                filled: true,
                fillColor: Colors.grey[200],
                suffixIcon: IconButton(
                  icon: Icon(Icons.search),
                  onPressed: () => _performSearch(_searchController.text),
                ),
                prefixIcon: IconButton(
                  icon: Icon(Icons.clear),
                  onPressed: _clearSearchHistory,
                ),
                border: OutlineInputBorder(
                  borderSide: BorderSide.none,
                  borderRadius: BorderRadius.circular(30),
                ),
              ),
            ),
          ),
          // 选项卡
          DefaultTabController(
            length: 2,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                TabBar(
                  tabs: [
                    Tab(text: '历史搜索'),
                    Tab(text: '搜索热词'),
                  ],
                ),
                Container(
                  height: 200,
                  child: TabBarView(
                    children: [
                      // 历史搜索列表
                      ListView.builder(
                        itemCount: _searchHistory.length,
                        itemBuilder: (context, index) {
                          return ListTile(
                            title: Text(_searchHistory[index]),
                            trailing: IconButton(
                              icon: Icon(Icons.clear, color: Colors.grey),
                              onPressed: () {
                                setState(() {
                                  _searchHistory.removeAt(index);
                                });
                              },
                            ),
                          );
                        },
                      ),
                      // 搜索热词列表
                      ListView.builder(
                        itemCount: _hotSearchWords.length,
                        itemBuilder: (context, index) {
                          return ListTile(
                            leading: Icon(Icons.whatshot, color: Colors.orange),
                            title: Text(_hotSearchWords[index]),
                          );
                        },
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}